/* Generic JavaScript utilities/hacks */

// (C) Douglas Crockford
String.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};

// (C) Douglas Crockford
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}; 

var SW = function () {

    var _getType = function (value) {
            var s = typeof value;
            if (s === 'object') {
                if (value) {
                    if (value instanceof Array) {
                        s = 'array';
                    }
                } else {
                    s = 'null';
                }
            }
            return s;
    };

    return {
        getType: _getType,

        include: function() {
            var argsnum = arguments.length;
            var scripts = [];
            
            if(argsnum==0) return false;
            
            var headTag = document.getElementsByTagName('head')[0]; 
            var fragment = document.createDocumentFragment();
            
            var arg,argType;
            for(var i=0; i<argsnum; i++) {
                arg = arguments[i];
                argType = _getType(arg);
                if (argType === 'string') {
                    scripts.push(arg);
                } else if (argType === ' array') {
                    var blen = arg.length;
                    for(var y=0; y<blen; y++) {
                        scripts.push(arg[y]);
                    }
                }
            }

            var slen = scripts.length;
            var el;
            for(i=0; i<slen; i++) {
                el = document.createElement('script');
                el.setAttribute('type','text/javascript');
                el.setAttribute('src',scripts[i]);
                fragment.appendChild(el);
            }

            headTag.appendChild(fragment);
        }
    }
}();


